/*********************************************************************************
 *      Copyright:  (C) 2017 luminqi
 *                  All rights reserved.
 *
 *       Filename:  select_sort.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2017年08月26日)
 *         Author:  luminqi <958822582@qq.com>
 *      ChangeLog:  1, Release initial version on "2017年08月26日 01时11分56秒"
 *                 
 ********************************************************************************/

#include <stdio.h>

#define SWAP(x, y, t) ((t)=(x),(x)=(y),(y)=(t))


void select_sort(int a[], int n);
int main(int argc, char **argv)
{
    int a[8] = {2,3,4,1,5,7,8,6};
    int i = 0;

    printf("before sort:\n");
    for (i=0; i<8; i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    
    select_sort(a, 8);
    printf("after sort:\n");
    for (i=0; i<8; i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");

    return 0;
}

void select_sort(int a[], int n)
{
    int i = 0;
    int j = 0;
    int min = 0;
    int tmp = 0;

    for (i=0; i<n-1; i++)
    {
        min = i;
        for (j=i+1; j<n; j++)
        {
            if (a[j]<a[min])
            {
                min = j;
            }
        }
        SWAP(a[i], a[min], tmp);
    }
    return;
}
